<template>
    <div>
        <a-modal
        centered
        title="选择职工"
        :width="1000"
        :visible="visible"
        @ok="handleOk"
        @cancel="handleCancel"
        cancelText="关闭">
            <!-- 查询区域 -->
            <div class="table-page-search-wrapper">
                <a-form layout="inline">
                <a-row :gutter="24">

                    <a-col :md="12" :sm="12">
                    <a-form-item label="职员姓名">
                        <a-input placeholder="" v-model="queryParam.name"></a-input>
                    </a-form-item>
                    </a-col>
                    <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                    <a-col :md="9" :sm="24">
                    <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 21px">查询</a-button>
                    <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                    </a-col>
                    </span>
                </a-row>
                </a-form>
            </div>

            <!-- table区域-begin -->
            <div>
                <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
                <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
                <a style="margin-left: 24px" @click="onClearSelected">清空</a>
                </div>

                <a-table
                ref="table"
                size="middle"
                :scroll="{x:true}"
                bordered
                rowKey="userId"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                :row-selection="rowSelection"
                class="j-table-force-nowrap"
                @change="handleTableChange">


                </a-table>
            </div>
        </a-modal>
    </div>
</template>

<script>
import {filterObj} from '@/utils/util'
import {getAction} from '@/api/manage'
export default {
    props:{
        radid: {
            type: String,
            default: ''
        },
    },
    name: "SelectPeopleForRoom",
    data () {
        return {
            visible: false,
            roomHasForPeople:[],
            dataSource:[],
            queryParam: {},
            loading: false,
            filters: {},
            selectedRowKeys: [],
            selectionRows: [],
            isorter: {
            column: 'createTime',
            order: 'desc'
            },
            ipagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '30'],
                showTotal: (total, range) => {
                    return range[0] + '-' + range[1] + ' 共' + total + '条'
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0
            },
            columns: [
            {
                title: '#',
                dataIndex: '',
                key:'rowIndex',
                width:60,
                align:"center",
                customRender:function (t,r,index) {
                return parseInt(index)+1;
                }
            },
            {
                title:'职员姓名',
                align:"center",
                dataIndex: 'realName'
            },
            {
                title:'手机号',
                align:"center",
                dataIndex: 'tel'
            },
            {
                title:'报名时间',
                align:"center",
                sorter: true,
                dataIndex: 'applyTime'
            },
            {
                title:'得分',
                align:"center",
                sorter: true,
                dataIndex: 'score'
            },
            ],
            url: {
                listofEnterUserOfDepart: "/checkemployee/znzfCheckEmployee/queryByRadIdAndApartId",
            },
        }
    },

    computed: {
        rowSelection() {
            return {
                selectedRowKeys: this.selectedRowKeys,
                onChange: this.onSelectChange,
                type:'radio',
                getCheckboxProps: record => ({
                    props: {
                        disabled: this.ishasRoom(record.userId), // Column configuration not to be checked
                        uid: record.userId,
                    },
                }),
            };
        },
    },

    created() {
        //this.loadData();
    },

    methods: {
        ishasRoom(uid){
            for(let item of this.roomHasForPeople){
                if(!item.uid || item.uid === ''){
                    continue;
                }
                if(item.uid === uid){
                    return true;
                }
            }
            return false;
        },
        getQueryParams() {
            //获取查询条件
            let sqp = {}
            var param = Object.assign(sqp, this.queryParam, this.isorter ,this.filters);
            param.field = this.getQueryField();
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return filterObj(param);
        },
        getQueryField() {
            //TODO 字段权限控制
            var str = "id,";
            this.columns.forEach(function (value) {
            str += "," + value.dataIndex;
            });
            return str;
        },

        loadData(arg) {
            if (!this.url.listofEnterUserOfDepart) {
                this.$message.error('请设置url.listofEnterUserOfDepart属性!')
                return
            }
            //加载数据 若传入参数1则加载第一页的内容
            if (arg === 1) {
            this.ipagination.current = 1
            }
            if (this.radid === '') return
            
            let params = this.getQueryParams()//查询条件
            params.radId = this.radid

            this.loading = true

            getAction(this.url.listofEnterUserOfDepart, params).then((res) => {
            if (res.success) {
                this.dataSource = res.result.records;
                if(res.result.total)
                {
                this.ipagination.total = res.result.total;
                }else{
                this.ipagination.total = 0;
                }
            }else{
                this.dataSource = [];
                this.ipagination.total = 0;
                this.$message.warning(res.message)
            }
            }).finally(() => {
                this.loading = false
            })
        },
        searchQuery() {
            this.loadData(1)
        },
        searchReset() {
            this.queryParam = {}
            this.loadData(1)
        },
        
        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys
            this.selectionRows = selectionRows
        },
        onClearSelected(){
            this.selectedRowKeys = []
            this.selectionRows = []
        },
        handleTableChange(pagination, filters, sorter) {
            //分页、排序、筛选变化时触发
            //TODO 筛选
            if (Object.keys(sorter).length > 0) {
            this.isorter.column = sorter.field
            this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
            }
            this.ipagination = pagination
            this.loadData()
        },
        
        handleOk(){
            if(this.selectionRows.length <= 0) {
                this.$message.warning('还未选择！');
            }
            
            console.log("data:" + this.selectionRows[0]);
            this.$emit("selectFinished", this.selectionRows[0]);
            this.visible = false;
        },
        handleCancel(){
            this.visible = false;
        },
        openThisModel(parentGive,nowUserId){
          this.selectedRowKeys = [];
          this.loadData();
          
          if(nowUserId && nowUserId !== '')
            this.selectedRowKeys = [nowUserId];
          this.roomHasForPeople = parentGive;
          this.visible = true;
        }
    }
}

</script>
<style scoped>
.ant-card-body .table-operator {
    margin-bottom: 18px;
  }

  .ant-table-tbody .ant-table-row td {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .anty-row-operator button {
    margin: 0 5px
  }

  .ant-btn-danger {
    background-color: #ffffff
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }
</style>